import { EffectsCommandMap, Subscription } from 'dva';
interface IGbobelState {
  isHeaderFloat: boolean;
}

interface IGlobalModel {
  namespace: string;
  state: IGbobelState;
  reducers: {
    setHeaderFloat(
      state: IGbobelState,
      action: { payload: boolean },
    ): IGbobelState;
    setPermission(
      state: IGbobelState,
      action: { payload: boolean },
    ): IGbobelState;
  };
  effects: {
    setHeaderFloatAsync(
      action: { payload: boolean },
      effects: EffectsCommandMap,
    ): Generator;
  };
  subscriptions: {
    listenScroll: Subscription;
    listenResize: Subscription;
    listRouter: Subscription;
  };
}

const globalModel: IGlobalModel = {
  namespace: 'global',
  state: {
    isHeaderFloat: false,
  },
  reducers: {
    setHeaderFloat(state, action) {
      return {
        ...state,
        isHeaderFloat: action.payload,
      };
    },
    setPermission(state, action) {
      return {
        ...state,
        isHeaderFloat: action.payload,
      };
    },
  },
  effects: {
    *setHeaderFloatAsync(action, { call, put, select }) {
      yield put({
        type: 'setHeaderFloat',
        payload: action.payload,
      });
    },
  },
  subscriptions: {
    listRouter({ dispatch, history }) {
      history.listen((pathname, query) => {
        // console.log('modeles listen router', pathname, query)
      });
    },

    listenScroll({ dispatch }) {},
    listenResize() {
      window.onresize = function () {
        // console.log('resize', window.innerHeight, window.innerWidth)
      };
    },
  },
};

export default globalModel;
